using MultiAgentsClient.Service.WorkflowEngine.BaseNodes;
using MultiAgentsShared;

namespace MultiAgentsClient.Service.WorkflowEngine.BuiltInNodes;

/// <summary>
/// No toolcall for llm switch request
/// </summary>
public class NodeLLMSwitchRequest(string nodeId) : NodeRequestToService(nodeId)
{
    public string Message = "";
    public List<string> Categories = [];
}

public class NodeLLMSwitchResponse(string nodeId) : NodeResponseFromService(nodeId)
{
    public string SwitchResult = "";
}

[NodeGraphPortDef(1, -1)]
public class LLMSwitchNode : ServiceDependentNode<NodeLLMSwitchRequest, NodeLLMSwitchResponse>
{
    public class Category
    {
        public string CategoryContent { get; set; } = "";
    }

    [NodeGraphParam] public string Message = "";
    [NodeGraphParam] public List<Category> Categories = [];
    private string MessageRuntime => InsertVariable(Message);

    [NodeGraphOutput] private string SwitchResult = "";

    public LLMSwitchNode() : base() { }

    protected override NodeOutcome ServiceResponseHandler(NodeLLMSwitchResponse cmd)
    {
        Debug.LogLine("LlmSwitchNodeResponse:" + cmd.SwitchResult);
        SwitchResult = cmd.SwitchResult;
        runtimeContext.SetVar(Id, () => SwitchResult);

        int index = Categories.Select(ca => ca.CategoryContent).ToList().FindIndex(s => s == SwitchResult);

        return new NodeOutcome(NodeOutcomeStatus.Success, index);
    }

    protected override void SendRequestToService()
    {
        NodeLLMSwitchRequest nodeLLMSwitchRequest = new(Id)
        {
            Message = MessageRuntime,
            Categories = Categories.Select(ca => ca.CategoryContent).ToList(),
        };

        EventBus.Publish(nodeLLMSwitchRequest);
    }
}
